#coding=utf8
import os
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.model_selection import train_test_split
import cPickle


model_name = 'Resnet50'
model_mode = 'finetuned_model'
ckpt_epock = 20
layer_name = 'flatten0'
X1 = np.load('CNNft/{0}/{1}_{2}/{3}/ft_train.npy'.format(model_name,model_mode,ckpt_epock,layer_name))
X_test1 = np.load('CNNft/{0}/{1}_{2}/{3}/ft_test.npy'.format(model_name,model_mode,ckpt_epock,layer_name))

model_name = 'inception_resnet_v2'
model_mode = 'finetuned_model'
ckpt_epock = 6000
layer_name = 'PreLogitsFlatten'
X2 = np.load('CNNft/{0}/{1}_{2}/{3}/ft_train.npy'.format(model_name,model_mode,ckpt_epock,layer_name))
X_test2 = np.load('CNNft/{0}/{1}_{2}/{3}/ft_test.npy'.format(model_name,model_mode,ckpt_epock,layer_name))

X = np.concatenate((X1,X2),axis=1)
# X = X1
y = pd.read_csv('CNNft/train_img_list.csv')['img_label_list']

print X.shape,y.shape

le = LabelEncoder()
y = le.fit_transform(y)

X_train,X_val,y_train,y_val = train_test_split(X,y,test_size=0.2,random_state=37,stratify=y)


# scale
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_val = scaler.transform(X_val)


# modeling
from sklearn.svm import SVC,LinearSVC
from xgboost import XGBClassifier
import xgboost as xgb
from sklearn.ensemble import RandomForestClassifier

# clf = SVC(C=1.0,kernel='rbf')
# clf = LinearSVC(C=0.01, random_state=42)
# clf = RandomForestClassifier(n_estimators=50,max_depth=5,criterion='gini',random_state=13)
# clf = XGBClassifier(max_depth=5,
#                     booster = 'bgtree',
#                     objective='multi:softmax',
#                     learning_rate=0.4,
#                     n_estimators=100,
#                     gamma=0.1,
#                     colsample_bytree=0.3,
#                     seed = 37,
#                     subsample = 0.7,
#                     silent=True)

params={
	'booster':'gbtree',
	'objective':'multi:softmax',
	'gamma':0.1,
	'max_depth':5,
	# 'lambda':70,
	'subsample':0.7,
	'colsample_bytree':0.3,
	'min_child_weight':0.3,
	'eta': 0.04,
	'seed':69,
	'silent':1,
    'num_class':100,
	}
#
dtrain = xgb.DMatrix(X_train, y_train)
clf_xgb = xgb.cv(params, dtrain, num_boost_round=500 , nfold=5,verbose_eval=True)
# num_boost_round = 100  #71
# clf = xgb.train(params, dtrain, num_boost_round)#,evals=watchlist)


# #train
# clf.fit(X_train,y_train)
# cPickle.dump(clf, open('concat_ft/concat_ft_model/xgb.pkl', 'wb'))
# cPickle.dump(clf,open('concat_ft/concat_ft_model/linearSVC_C001.pkl','wb'))
# clf = cPickle.load(open('concat_ft/concat_ft_model/linearSVC_C1.pkl','rb'))
clf = cPickle.load(open('concat_ft/concat_ft_model/xgb.pkl','rb'))

# validation
# y_pred = clf.predict(X_val)
y_pred = np.round(clf.predict(xgb.DMatrix(X_val)))

from sklearn.metrics import classification_report,accuracy_score
print accuracy_score(y_val, y_pred, normalize=True, sample_weight=None)

# test

X_test = np.concatenate((X_test1,X_test2),axis=1)
X_test = scaler.transform(X_test)
print('X_test shape:{0}'.format(X_test.shape))

# online_pred = clf.predict(X_test)

online_pred = np.round(clf.predict(xgb.DMatrix(X_test))).astype(int)
online_pred = le.inverse_transform(online_pred)
print online_pred.shape
print len(np.unique(online_pred)),np.max(np.unique(online_pred))


img_name_list = pd.read_csv('CNNft/test_img_list.csv')['img_path_list'].str.split('/').str[-1] \
                                                                        .str.split('.').str[0]

result = pd.DataFrame({'label':online_pred,'img_name': img_name_list.astype(str)})

# result[['label','img_name']].to_csv('concat_ft/results/XGB_Res50(ft)_IncepResv4(ft).txt',
#                                     index=False,header=None,sep='\t')